package modellayer;

import java.util.ArrayList;


/**
 * The class Product is to hold information about a given product, of which a object is to be
 * saved in the ProductContainer class.
 * 
 * @author Allan Eriksen, Christian Stidsborg, Andreas Rye, Casper Madsen, Stefan Bols 
 * @version 051213
 */
public class Product
{
    private static int counter; // This counter keeps track of how many clerk objects
    // there has been initialized and creates an uniqe clerkNo for each clerk
    private String barcode;
    private int productNo;
    private double price;
    private String productLabel;
    private ArrayList<ProductCopy> copiesList;

    public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public void setProductLabel(String productLabel) {
		this.productLabel = productLabel;
	}

    /**
     * This function creates a new Product object and creates a new ArrayList for the product.
     * @param String barcode, double price, String productLabel
     */
    /*public Product(String barcode, double price, String productLabel)
    {
        this.barcode = barcode;
        this.counter++;
        this.productNo = counter;
        this.price = price;
        this.productLabel = productLabel;
        copiesList = new ArrayList<ProductCopy>();
        
        // TEST USE (for each product there will be 10 test copies):
        addProductCopies(10);
    }*/
    /**
     * This method creates a product and takes all the necessary parameters to it, the last
     * parameter cointains the number of copies there is to be added
     * @param barcode The barcode for the product
     * @param price The products price including VAT/TAX
     * @param productLabel Products name/description
     */
    public Product(String barcode, double price, String productLabel, int numberOfCopies)
    {
        this.barcode = barcode;
        counter++;
        this.productNo = counter;
        this.price = price;
        this.productLabel = productLabel;
        copiesList = new ArrayList<ProductCopy>();
        
        if (numberOfCopies>0) {
        	addProductCopies(numberOfCopies);
        }
    }
    
    public boolean addProductCopy()
    {
    	ProductCopy p = new ProductCopy(this);
    	
    	copiesList.add(p);
    	
    	return true;
    }
    public boolean addProductCopy(ProductCopy copy)
    {
    	copiesList.add(copy);
    	
    	return true;
    }
    
    public boolean addProductCopies(int numberOfCopies) {
    	boolean success = false;
    	
    	for (int i = 0; i < numberOfCopies; i++) {
    		success = this.addProductCopy();
    	}
    	
    	return success;
    }
    
    public ArrayList<ProductCopy> getAllProductCopies()
    {
    	ArrayList<ProductCopy> allProductCopies = null;
    	
    	for (ProductCopy p : copiesList)
    	{
    		allProductCopies.add(p);
    	}
    	
    	return allProductCopies;
    }
    
    public ProductCopy getProductCopy(int copyNo)
    {
    	ProductCopy result = null;
    	boolean found = false;
    	int i = 0;
    	int listSize = listSize();
    	
    	while (i<listSize && !found)
    	{
    		if (copiesList.get(i).getCopyNo()==copyNo)
    		{
    			result = copiesList.get(i);
    			found = true;
    		}
    		i++;
    	}
    	
    	return result;
    }
    
    public int listSize()
    {
    	int listSize = copiesList.size();
    	
    	return listSize;
    }
    
    /**
     * Accessor method.
     * @Returns barcode
     */
    public String getBarcode()
    {
        return barcode;
    }
    
    /**
     * Accessor method.
     * @Returns copy
     */
    public ProductCopy getProductCopy()
    {
        ProductCopy copy = null;
        
        if (copiesList.size()!=0) 
        {
            copy = copiesList.get(0);
            copiesList.remove(copy);
        }
        
        return copy;
    }
    
    /*
     * This function is an iterator, it utilizes a sweep/search pattern to find the object.
     * @Param int copyNo
     *
    public void removeProductCopy(int copyNo)
    {
        Iterator<ProductCopy> it = copiesList.iterator();
        boolean found = false;
        
        while (it.hasNext() && !found) {
            ProductCopy copy = it.next();
            
            if (copy.getCopyNo()==copyNo) {
                it.remove();
                found = true;
            }
        }
    }
    
    /*
     * This function searches/sweeps an ArrayList for a copy.
     * @Param int copyNo
     * @Returns returnIsIt
     *
    public boolean isCopyHere(int copyNo)
    {
        boolean returnIsIt = false;
        int listSize = copiesList.size();
        
        for (int i = 0; i < listSize && !returnIsIt; i++) {
            if(copiesList.get(i).getCopyNo()==copyNo) {
                returnIsIt = true;
            }
        }
        
        return returnIsIt;
    }*/
    
    /**
     * Accessor method.
     * @Returns productNo
     */
    public String getProductNo()
    {
    	String add = "";
		if (productNo < 10) {
			add = "00000";
		} else if (productNo < 100) {
			add = "0000";
		} else if (productNo < 1000) {
			add = "000";
		} else if (productNo < 10000) {
			add = "00";
		} else if (productNo < 100000) {
			add = "0";
		} else if (productNo < 1000000) {
			add = "";
		} else {
			add = "err:";
		}
		return add+productNo;
    }
    
    /**
     * Accessor method.
     * @Returns price
     */
    public double getPrice()
    {
        return price;
    }
    
    /**
     * Accessor method.
     * @Returns price
     */
    public double getPriceNoTax()
    {
    	double returnVal = price-getTax();
    	
        return returnVal;
    }
    
    /**
     * Accessor method.
     * @Returns price
     */
    public double getTax()
    {
    	double returnVal = price*0.2;
    	
        return returnVal;
    }
    /**
     * Accessor method
     * @Returns productLabel
     */
    public String getProductLabel()
    {
        return productLabel;
    }
}
